package io.renren.modules.project.controller;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.common.annotation.SysLog;
import io.renren.common.exception.RRException;
import io.renren.common.utils.*;
import io.renren.modules.oss.cloud.OSSFactory;
import io.renren.modules.oss.constant.UploadConstant;
import io.renren.modules.oss.entity.SysOssEntity;
import io.renren.modules.oss.service.FileUpLoadSrv;
import io.renren.modules.project.entity.PojectOperationEntity;
import io.renren.modules.project.service.PojectOperationService;
import jdk.nashorn.internal.parser.JSONParser;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.project.entity.PojectWorkitemEntity;
import io.renren.modules.project.service.PojectWorkitemService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import static io.renren.common.utils.FileUtil.input2byte;


/**
 * 
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-10-29 16:45:07
 */
@RestController
@RequestMapping("/generator/pojectworkitem")
public class PojectWorkitemController {
	@Autowired
	private PojectWorkitemService pojectWorkitemService;
	@Autowired
	private FileUpLoadSrv fileUpLoadSrv;
	@Autowired
	private PojectOperationService pojectOperationService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("generator:pojectworkitem:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<PojectWorkitemEntity> pojectWorkitemList = pojectWorkitemService.queryList(query);
		int total = pojectWorkitemService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(pojectWorkitemList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("generator:pojectworkitem:info")
	public R info(@PathVariable("id") String id){
		PojectWorkitemEntity pojectWorkitem = pojectWorkitemService.queryObject(id);
		
		return R.ok().put("pojectWorkitem", pojectWorkitem);
	}
	
	/**
	 * 保存
	 */
	@SysLog("新增工作项")
	@RequestMapping("/save")
	@RequiresPermissions("generator:pojectworkitem:save")
	public R save(@RequestBody PojectWorkitemEntity pojectWorkitem){
		pojectWorkitemService.save(pojectWorkitem);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改工作项")
	@RequestMapping("/update")
	@RequiresPermissions("generator:pojectworkitem:update")
	public R update(@RequestBody PojectWorkitemEntity pojectWorkitem){
		pojectWorkitemService.update(pojectWorkitem);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("generator:pojectworkitem:delete")
	public R delete(@RequestBody String[] ids){
		pojectWorkitemService.deleteBatch(ids);

		return R.ok();
	}

	/**
	 * @param file 上传的文件
	 * @param pojectOperationEntity 附件实体
	 * 任务工作项附件上传
	 */
	@RequestMapping("/upload")
	public R upload(@RequestParam("file") MultipartFile[] file, PojectOperationEntity pojectOperationEntity, HttpServletRequest request) throws Exception {
		System.out.println("***********************进入upload");
		System.out.println("********"+file.length);
		if (file[0].isEmpty()) {
			throw new RRException("上传文件不能为空");
		}
		String fileName = file[0].getOriginalFilename();	// 文件名
		System.out.println("**********文件名*************" + fileName);
		Map<String, String> map = new HashMap<String, String>();
		try {
			map.putAll(fileUpLoadSrv.write(file[0], ""));
			//System.out.println("**********文件路径*************:" + map.get("filePath"));
			pojectOperationEntity.setId(RandomId.createId()+"");
			Date currentTime = new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			String dateString = formatter.format(currentTime);
			pojectOperationEntity.setOperationDate(dateString);
			pojectOperationEntity.setOperationName(fileName);
			pojectOperationEntity.setAttachPath(map.get("filePath"));
			pojectOperationService.save(pojectOperationEntity);
			return R.ok().put("success","上传成功");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return R.error().put("error",e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			return R.error().put("error",e.getMessage());
		}
	}
}
